//
//  MainViewController.swift
//  WeiBo2017
//
//  Created by 王璐 on 2017/4/12.
//  Copyright © 2017年 wl. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
        
        setupTabBar()
        
        showNewFeatureView()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        setupComposeButton()
    }
    
    //MARK-设置按钮的位置
    fileprivate func setupComposeButton(){
        
        tabBar.bringSubview(toFront: composeButton)
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count )
        composeButton.frame = tabBar.bounds.insetBy(dx: width * 2, dy: 0)
        
        
    }
    
    //按钮的点击事件
  @objc  fileprivate func composeButtonClick(){
        
        print("点击加号按钮")
    }
    
    //MARK-懒加载加号按钮
    lazy var composeButton : UIButton = UIButton(
        title: nil,
        imageName:"tabbar_compose_icon_add",
        backgroudImageName: "tabbar_compose_button")
}

// MARK: - 显示新特性界面
extension MainViewController{
    
    fileprivate func showNewFeatureView(){
        
        //如果没有登录 直接返回
        if  !UserCount.sharedUserCount.isLogin {
            return
        }
        //如果登录了判断是否有新版本
        let v = isNewVersion ? NewFeatureView():WelcomeView()
        
        self.view.addSubview(v)
        
    }
    
    ///是否有新版本
    fileprivate var isNewVersion : Bool {
        
        //获取当前版本
        let currentVersion = Bundle.wl_currentVersion

        //VersionKEY
        let versionKey = "wlVersionKEY"
        //获取之前的版本号
        let sandBoxVersion = UserDefaults.standard.string(forKey: versionKey) ?? ""
        //将之前的版本保存在沙盒中
        UserDefaults.standard.setValue(currentVersion , forKey: versionKey)
         print("是否新版本\(currentVersion != sandBoxVersion )")
        //返回两个版本的比较
        return (currentVersion != sandBoxVersion)
    }

}

//MARK-设置tabbar 加号按钮 和 画线
extension MainViewController{
    
    func setupTabBar(){
        tabBar.shadowImage = UIImage.wl_singleDotImage(color: UIColor(white:0.9,alpha:0))
        tabBar.backgroundImage = UIImage(named:"tabbar_background")
        tabBar.addSubview(composeButton)
        composeButton.addTarget(self, action: #selector(MainViewController.composeButtonClick), for: .touchUpInside)
        
    }
    
}

 //MARK - 添加子控制器
extension MainViewController{
    
    fileprivate func addChildViewControllers() {
        
        addChildViewController(vc: HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(vc: MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(vc: DiscoveryViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(vc: ProfileViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    
    fileprivate  func addChildViewController(vc:UIViewController,title:String,imageName:String) {
        
        vc.title = title
    
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
    
        vc.tabBarItem.image = UIImage(named:imageName)
    
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
    
        let nav = UINavigationController(rootViewController : vc)
        
        addChildViewController(nav)
        
    }
    
}
